package insight.google.game.era.bo.policy.gamecard;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import insight.google.game.era.bo.GameActionLog;
import insight.google.game.era.bo.GameRoom;
import insight.google.game.era.bo.GameUser;
import insight.google.game.era.bo.card.GameCard;
import insight.google.game.era.bo.policy.AbstractPolicy;
import insight.google.game.era.client.proxy.PlayerClientProxy;
import insight.google.game.era.client.proxy.PlayerClientProxyImpl;
import insight.google.game.era.core.Util;
import insight.google.game.era.mgr.GameLogManager;

public class DefendHorsePolicy extends AbstractPolicy {

    /**
	 * 
	 */
	private PlayerClientProxy playerClientProxy;

	@Override
	public boolean execute(GameUser original, GameUser[] targets, Map params) {
		// TODO Auto-generated method stub

        playerClientProxy = PlayerClientProxyImpl.getInstance();

        //get js component id
        String jsComponentId = (String)params.get("jsComponenetId");

        GameRoom room = Util.getRoom();
		GameCard card = room.getCardByCardName("defendhorse");
		if(original.checkSameTypeEquipment(card)){
//			playerClientProxy.replaceEquipmentCard(original.getScriptProxy(), jsComponentId, card);
			original.replaceEquipmentCard(card, jsComponentId);
		}
		else{
			original.addEquipmentCards(card);
			original.setDefendDistance(original.getDefendDistance() + card.getDistance());
		}
		GameLogManager gameLogManager = room.getGameLogManager();
        gameLogManager.addGameLog(new GameActionLog(original.getName() + " has played defend horse."));

        Collection<GameUser> players = room.getUserList();
        Collection<GameUser> attackUsers = null;
        List<String> userList = new ArrayList<String>();
        for (GameUser user : players) {
        	if(!user.getName().equals(original.getName())){
        		attackUsers = room.getAttackableUsers(user);
        		userList = new ArrayList<String>();
        		for (GameUser attackuser : attackUsers) {
        			userList.add(attackuser.getName());
        		}
        		user.setAttackUserList(userList);
        		user.setRegisterAttack(true);
//                playerClientProxy.registerAttackableUser(user.getScriptProxy(), jsComponentId, attackUsers);
//        		original.registerAttackableUser(jsComponentId);
        	}
        }

//        playerClientProxy.setActive(original.getScriptProxy(), jsComponentId, true);
        
        original.refreshEquipmentCard(jsComponentId);
        original.setClientActive(true, jsComponentId);
        return true;
	}

}
